﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TestHelper.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the TestHelper type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Collaboris.Saf.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Reflection;

    using Utils;

    public static class TestHelper
    {
        /// <summary>
        /// Runs the MS build target.
        /// </summary>
        /// <param name="projectPath">The project path.</param>
        /// <param name="target">The target.</param>
        public static void RunMSBuildTarget(string projectPath, string target)
        {
            Process buildProcess = new Process();
            if (File.Exists("C:\\Windows\\Microsoft.NET\\Framework64\\v3.5\\SAF.exe"))
            {
                buildProcess.StartInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework64\\v3.5\\SAF.exe";
            }
            else
            {
                if (File.Exists("C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\SAF"))
                {
                    buildProcess.StartInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\SAF";
                }
                else
                {
                    throw new FileNotFoundException("Cannot find SAF in either the 32 or 64 bit usual places.");
                }
            }

            buildProcess.StartInfo.Arguments = String.Format("{0} /target:{1}", projectPath, target);
            buildProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            buildProcess.StartInfo.CreateNoWindow = true;
            buildProcess.Start();
            buildProcess.WaitForExit();
            buildProcess.Close();
        }

        /// <summary>
        /// Copies the support files.
        /// </summary>
        public static void CopySupportFiles()
        {
            if (!Directory.Exists(TestGlobals.WorkingDirectory))
            {
                Directory.CreateDirectory(TestGlobals.WorkingDirectory);
            }

            FileHelper.CopyFolders(GetTestDirectory(), TestGlobals.WorkingDirectory);
        }

        /// <summary>
        /// Gets the test directory.
        /// </summary>
        public static string GetTestDirectory()
        {
            string currentDir = Directory.GetCurrentDirectory();
            DirectoryInfo directory = Directory.GetParent(currentDir);
            if (directory.Parent.FullName != null)
            {
                return Path.Combine(directory.Parent.FullName, "Support");
            }

            return string.Empty;
        }

        public static List<string> GetMacroToTest(string xmlFileName)
        {
            string loc = "assembly://Collaboris.Saf.Tests," +
                                          "Version=2.0.0.0," +
                                          "Culture=neutral," +
                                          "PublicKeyToken=182db3eac6a9e195/" +
                                          "Collaboris.Saf.Tests.Support.DynamicMacros/" + xmlFileName;

            List<string> arr = new List<string>();
            arr.Add(loc);
            return arr;
        }


    }
}